home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
Think Class Libraries
/
TP TCL->CW TCL v1.1.2.3
/
UPI ƒ
/
Porting to UPIs ƒ
/
Sane2FP.p
< prev
Wrap
Text File
|
1996-04-20
|
2KB
|
109 lines
unit Sane2FP; {v1.0, by Vik Rubenfeld, 1996. Freely distributable.}
interface
Uses FP, Types, TextUtils;
type
DecStr = string[255];
cString = packed array[1..256] of char;
procedure Num2Str(DF: DecForm; R: double_t; var S: DecStr);
FUNCTION Str2Num(s: Str255): Double_t;
Function Num2Integer(R: double_t): integer;
function ItsANaN (R: double_t): Boolean;
function GetMeAnNaN: double_t;
Function Num2LongInt(R: double_t): LongInt;
function TruncForLongInts(R: double_t): LongInt;
implementation
procedure Num2Str(DF: DecForm; R: double_t; var S: DecStr);
var
DecimalRec: decimal;
CS: cString;
theStringPtr: StringPtr;
begin
num2dec(DF, R, DecimalRec);
dec2str(DF, DecimalRec, @CS);
theStringPtr:= c2pstr(@CS);
S:= Str255(CS);
end;
FUNCTION Str2Num(s: Str255): Double_t;
VAR
DecimalRec: Decimal;
CSPtr: ConstCStringPtr;
anIndex: INTEGER;
aValidPrefix: INTEGER; { ignore }
BEGIN
CSPtr := P2CStr(@S);
anIndex:= 0;
str2dec( CSPtr, anIndex, DecimalRec, aValidPrefix );
Str2Num := dec2num( DecimalRec );
END;
Function Num2Integer(R: double_t): integer;
var
DecimalRec: decimal;
DF: DecForm;
begin
DF.Style:= FixedDecimal;
DF.Digits:= 99;
num2dec(DF, R, DecimalRec);
Num2Integer:= dec2s(DecimalRec);
end;
Function Num2LongInt(R: double_t): LongInt;
var
DecimalRec: decimal;
DF: DecForm;
begin
DF.Style:= FixedDecimal;
DF.Digits:= 99;
num2dec(DF, R, DecimalRec);
Num2LongInt:= dec2l(DecimalRec);
end;
function ItsANaN (R: double_t): Boolean;
var
DecimalRec: decimal;
DF: DecForm;
theClass: integer;
begin
{DF.Style:= FixedDecimal;
DF.Digits:= 99;
num2dec(DF, R, DecimalRec);
ItsANaN:= DecimalRec.Sig[0] = 'N';}
theClass:= __fpclassifyd(R);
ItsANaN:= not (theClass in [FP_ZERO, FP_NORMAL]);
end;
function GetMeAnNaN: double_t;
var
S: DecStr;
theConstCStringPtr: ConstCStringPtr;
begin
S:= '32';
theConstCStringPtr:= ConstCStringPtr(P2CStr(@S));
GetMeAnNaN := NaN(theConstCStringPtr);
end;
{on 68k machines Trunc returns an integer. This routine is helpful if you need to
return a LongInt}
function TruncForLongInts(R: double_t): LongInt;
begin
R:= Floor(R);
TruncForLongInts:= Num2LongInt(R);
end;
end.